//
//  DepartmentViewController.m
//  安好
//
//  Created by lianchuang on 14-12-11.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "DepartmentViewController.h"
#import "RegistrationDepartmentModal.h"
//选好科室跳转到医生列表页面
#import "RegDoctorListViewController.h"

#define IPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)

@interface DepartmentViewController ()<UITableViewDataSource,UITableViewDelegate>

/**
 *  左边的table
 */
@property(nonatomic,strong)UITableView * LeftTableView;
/**
 *  右边的table
 */
@property(nonatomic,strong)UITableView * RightTableView;

/**
 *  右边的数据
 */
@property(nonatomic,strong)NSMutableArray * rightDataArray;
/**
 * 左边的数据
 */
@property(nonatomic,strong)NSMutableArray * leftDataArray;

/**
 * 左table页面选中的 父科室modal
 */
@property(nonatomic,strong)RegistrationDepartmentModal * departmentModal;
/**
 *  获取父级的科室id，选中子级科室的时候就把子科室id得到
 */
@property(nonatomic,copy)NSString * hospdeptid;

@end

@implementation DepartmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeNavBar];
    [self loadData];
    [self AFNetRequestGetDepartment];
    [self makeTableView];
    
    
}

#pragma mark - 导航条设置
-(void)makeNavBar
{
    self.navigationItem.title = @"科室列表";
    //左按钮--返回
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    [backBtnImg setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}

-(void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)loadData
{
    self.leftDataArray = [NSMutableArray arrayWithCapacity:0];
    self.rightDataArray = [NSMutableArray arrayWithCapacity:0];

}
#pragma mark - 获取某医院下面的科室
//在上面的请求中获取到医院，在这个请求中获取到该医院下面的科室，传参数医院id
-(void)AFNetRequestGetDepartment
{
    [self showActionView];
    
    NSString *postURL = [NSString stringWithFormat:@"%@wiki/hospitalDepartment",kBaseURL];
    //post参数列表
    NSDictionary *parameters = @{
                                 @"platform": kPlatform,
                                 @"version":kVersion,
                                 @"hospitalid":self.hospitalid,
                                 @"parenthosdepid":@"0"
                                 };
    //网络请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //开始请求
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        [self hiddenActionView];
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
            if (self.leftDataArray == nil) {
                [self shouLabel:@"暂无内容"];
            }
        }else
        {
            self.LeftTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
            
            MyNSLog("%@", responseObject[@"data"]);
            NSArray * deskArray = responseObject[@"data"];
            for (NSDictionary * dic in deskArray) {
                RegistrationDepartmentModal * modal = [[RegistrationDepartmentModal alloc] initWithDic:dic];
                [self.leftDataArray addObject:modal];
            }
            [self.LeftTableView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyNSLog("失败");
        [self hiddenActionView];
        
    }];
    
}
#pragma mark - 获取二级科室
//在上面的请求中获取到医院，在这个请求中获取到该医院下面的科室，传参数医院id
-(void)AFNetRequestGetChildDepartment
{
    [self showActionView];
    
    NSString *postURL = [NSString stringWithFormat:@"%@wiki/hospitalDepartment",kBaseURL];
    
    //post参数列表
    NSDictionary *parameters = @{@"platform": kPlatform,
                                 @"version":kVersion,
                                 @"hospitalid":self.hospitalid,
                                 @"parenthosdepid":self.hospdeptid
                                 };
    //网络请求管理者
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    //开始请求
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("成功");
        [self hiddenActionView];
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
            if (self.rightDataArray == nil) {
                [self shouLabel:@"暂无内容"];
            }
        }else
        {
            self.RightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            
            MyNSLog("%@", responseObject[@"data"]);
            NSArray * deskArray = responseObject[@"data"];
            for (NSDictionary * dic in deskArray) {
                RegistrationDepartmentModal * modal = [[RegistrationDepartmentModal alloc] initWithDic:dic];
                [self.rightDataArray addObject:modal];
            }
            [self.RightTableView reloadData];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        MyNSLog("失败");
        [self hiddenActionView];
        
    }];
    
}
-(void)makeTableView
{
    self.LeftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-iOS7TopMargin)style:UITableViewStyleGrouped];
    [self.view addSubview:self.LeftTableView];
    self.LeftTableView.delegate = self;
    self.LeftTableView.dataSource = self;
    self.LeftTableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    //隐藏横向和竖向的滑条
    self.LeftTableView.showsHorizontalScrollIndicator = NO;
    self.LeftTableView.showsVerticalScrollIndicator = NO;
    self.LeftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    //左边的table
    self.RightTableView = [[UITableView alloc] initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight-iOS7TopMargin)style:UITableViewStyleGrouped];
    [self.view addSubview:self.RightTableView];
    self.RightTableView.delegate = self;
    self.RightTableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.RightTableView.showsHorizontalScrollIndicator = NO;
    self.RightTableView.showsVerticalScrollIndicator = NO;
    self.RightTableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.RightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.RightTableView.layer.backgroundColor =[ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16].CGColor;
    
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.LeftTableView) {
        return self.leftDataArray.count;
    }else if (tableView == self.RightTableView) {
        return self.rightDataArray.count;
    }else
    {
        return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (IPhone6Plus) {
        return 53;
    }else{
        return 44;
    }
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.LeftTableView) {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LeftID"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LeftID"];
    }
    cell.textLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT];
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
        RegistrationDepartmentModal * modal = self.leftDataArray[indexPath.row];
        NSString * subString = modal.departmentname;
        for (NSInteger i = 0; i < subString.length; i++) {
            char ch = [subString characterAtIndex:i];
            if (ch != '\n' && ch != '\r') {
                subString = [subString substringFromIndex:i];
                break;
            }
        }
        cell.textLabel.text = subString;


    return cell;
        
    }
    else
    {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RightID"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RightID"];
        }
        cell.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
        cell.textLabel.font = [UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        RegistrationDepartmentModal * modal = self.rightDataArray[indexPath.row];
        NSString * subString = modal.departmentname;
        for (NSInteger i = 0; i < subString.length; i++) {
            char ch = [subString characterAtIndex:i];
            if (ch != '\n' && ch != '\r') {
                subString = [subString substringFromIndex:i];
                break;
            }
        }
        cell.textLabel.text = subString;
    
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    //点击的时左边的tableView--选父级科室
    if (tableView == self.LeftTableView) {

        RegistrationDepartmentModal * parModal =self.leftDataArray[indexPath.row];
        [UIView animateWithDuration:0.25 animations:^{
            self.LeftTableView.frame = CGRectMake(0, 0, kScreenWidth/2.0-20, kScreenHeight-iOS7TopMargin);
            self.RightTableView.frame = CGRectMake(self.LeftTableView.right, 0, kScreenWidth/2.0+20, kScreenHeight-iOS7TopMargin);
        }];
        [self.rightDataArray removeAllObjects];
        self.hospdeptid = parModal.hospdeptid;
        
        MyNSLog("选中父级科室id ：%@",self.hospdeptid);
        [self AFNetRequestGetChildDepartment];
//        [self.RightTableView reloadData];
    
    }else//点击选中子级科室
    {
        RegistrationDepartmentModal * modal = self.rightDataArray[indexPath.row];
        MyNSLog("%@", modal.departmentname);
        RegDoctorListViewController * docList = [[RegDoctorListViewController alloc] init];
        docList.regHosDepartDic = @{@"hospitalid":self.hospitalid,@"hospdeptid":modal.hospdeptid};
        MyNSLog("%@", docList.regHosDepartDic);
        [self.navigationController pushViewController:docList animated:YES];
    
    
    }


}
@end
